Programming Questions for Practice
Write a program to shift the given array towards left by N number of places. Fill the ZERO in the right most N places.
#include <stdio.h>
#include <stdlib.h>
/* Write a program to shift the given array towards left by N number of places.
Fill the ZERO in the right most N places. */
void displayArray(int array[], int length) {
printf("Array is\n");
for(int i = 0; i < length; i++)
printf("%d ", array[i]);
}
int main()
{
// Creating an array of given length
int length;
printf("Enter the length of the array: ");
scanf("%d", &length);
int *array = (int *)malloc(length*sizeof(int));
printf("Enter any %d numbers: ", length);
for(int i = 0; i < length; i++)
scanf("%d", &array[i]);
displayArray(array, length);
// Reading the number places to be shifted
int N;
printf("\nEnter the number of places: ");
scanf("%d", &N);
// Logic of shifting towards left
for(int counter = 1; counter<=N; counter++) {
int temp = array[length-1], i;
for(i=0; i < length; i++) {
array[i] = array[i+1];
}
array[i-1] = 0;
}
// Display the result
printf("\nAfter the left shift by %d places\n", N);
displayArray(array, length);
return 0;
}
Code not available...
Code not available...